<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input
          v-model="queryParams.ipAddress"
          placeholder="请输入IP地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="MAC地址" prop="macAddress">
        <el-input
          v-model="queryParams.macAddress"
          placeholder="请输入MAC地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
<!--      <el-form-item label="设备来源" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="请选择设备来源"
          clearable
          class="!w-240px"
        >
          <el-option label="手动添加" value="" />
        </el-select>
      </el-form-item>-->
<!--      <el-form-item label="接入时间" prop="lastOnlineTime">
        <el-date-picker
          v-model="queryParams.lastOnlineTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="离线时间" prop="lastOfflineTime">
        <el-date-picker
          v-model="queryParams.lastOfflineTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="接入时长" prop="onlineHour">
        <el-input
          v-model="queryParams.onlineHour"
          placeholder="请输入接入时长"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="设备状态" prop="deviceStatus">
        <el-select
          v-model="queryParams.deviceStatus"
          placeholder="请选择设备状态"
          clearable
          class="!w-240px"
        >
          <el-option label="在线" value="1" />
          <el-option label="离线" value="2" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="状态（0正常 1停用）" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态（0正常 1停用）"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备用户名" prop="deviceUsername">
        <el-input
          v-model="queryParams.deviceUsername"
          placeholder="请输入设备用户名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备密码" prop="devicePassword">
        <el-input
          v-model="queryParams.devicePassword"
          placeholder="请输入设备密码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="CPU使用率" prop="deviceCpu">
        <el-input
          v-model="queryParams.deviceCpu"
          placeholder="请输入CPU使用率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内存使用率" prop="deviceMemory">
        <el-input
          v-model="queryParams.deviceMemory"
          placeholder="请输入内存使用率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="磁盘" prop="deviceDisk">
        <el-input
          v-model="queryParams.deviceDisk"
          placeholder="请输入磁盘"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="设备别名" prop="deviceAlias">
        <el-input
          v-model="queryParams.deviceAlias"
          placeholder="请输入设备别名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备位置" prop="deviceLocation">
        <el-input
          v-model="queryParams.deviceLocation"
          placeholder="请输入设备位置"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['security:device:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
<!--        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['security:device:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="id" width="60"/>
      <el-table-column label="IP地址" align="center" prop="ipAddress" />
<!--      <el-table-column label="MAC地址" align="center" prop="macAddress" />-->
<!--      <el-table-column label="设备来源" align="center" prop="sourceType" />-->
      <el-table-column label="用户名" align="center" prop="deviceUsername" />
<!--      <el-table-column label="密码" align="center" prop="devicePassword" />-->
      <el-table-column label="CPU" align="center" prop="deviceCpu" width="80">
        <template #default="scope">
          {{ scope.row.deviceCpu ? scope.row.deviceCpu + '%' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="内存" align="center" prop="deviceMemory" width="80">
        <template #default="scope">
          {{ scope.row.deviceMemory ? scope.row.deviceMemory + '%' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="磁盘" align="center" prop="deviceDisk" width="80" >
        <template #default="scope">
          {{ scope.row.deviceDisk ? scope.row.deviceDisk + '%' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="设备别名" align="center" prop="deviceAlias" />
      <el-table-column label="设备位置" align="center" prop="deviceLocation" />
      <el-table-column
        label="最后接入时间"
        align="center"
        prop="lastOnlineTime"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column
        label="最后离线时间"
        align="center"
        prop="lastOfflineTime"
        :formatter="dateFormatter"
        width="180px"
      />-->
<!--      <el-table-column label="接入总时长" align="center" prop="onlineHour" />-->
      <el-table-column label="设备状态" align="center" prop="deviceStatus" >
        <template #default="scope">
          {{ scope.row.deviceStatus === '1' ? '在线' : '离线' }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="warning"
            @click="handleAccess(scope.row.ipAddress, scope.row.deviceUsername, scope.row.devicePassword)"
            v-hasPermi="['security:device:update']"
          >
            访问
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['security:device:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['security:device:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DeviceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DeviceApi, DeviceVO } from '@/api/security/device'
import DeviceForm from './DeviceForm.vue'

/** 设备接入 列表 */
defineOptions({ name: 'SecurityDevice' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DeviceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  ipAddress: undefined,
  macAddress: undefined,
  sourceType: undefined,
  lastOnlineTime: [],
  lastOfflineTime: [],
  onlineHour: undefined,
  deviceStatus: undefined,
  status: undefined,
  remark: undefined,
  createTime: [],
  deviceUsername: undefined,
  devicePassword: undefined,
  deviceCpu: undefined,
  deviceMemory: undefined,
  deviceDisk: undefined,
  deviceAlias: undefined,
  deviceLocation: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceApi.getDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceApi.deleteDevice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleAccess = async (ipAddress: string, deviceUsername: string, devicePassword: string) => {
  try {
    // 删除的二次确认
    //await message.delConfirm()
    // 发起删除
    //await DeviceApi.deleteDevice(id)
    //message.success(t('common.delSuccess'))
    // 刷新列表
    //await getList()
    window.open(`http://${ipAddress}/index.php?u=${deviceUsername}&p=${devicePassword}`, '_blank');
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DeviceApi.exportDevice(queryParams)
    download.excel(data, '设备接入.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
